Package vg.logging

Source Code of vg.logging.FileLog

package vg.logging;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import vg.core.ILog;
import vg.core.exception.EnumCriticalityException;
import vg.core.exception.LogException;

/**
* This class realizes interface ILog.
* Messages saves in file log.txt. And saves old log file in directory log.
* @author tzolotuhin
*/
public class FileLog implements ILog {
  private DataOutputStream output;
  private String fileName;
  private final SimpleLog simpleLog;
  //-------------------------------------------------------------------------
  public FileLog() throws LogException {
    this.simpleLog = new SimpleLog();
    File dir = new File("log");
    if(!dir.exists() || (dir.isFile() && !dir.isFile())) {
      File f = new File("log");
      try {
        f.mkdir();
      } catch (Exception ex) {
        throw(new LogException(ex.getMessage(), EnumCriticalityException.FAILED));
      }
    }
    Date data = new Date();
    long time = data.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("dd.M.yyyy H.mm.ss");
    String timeS = sdf.format(time);
    FileOutputStream f = null;
    try {
      f = new FileOutputStream("log/"+timeS+"_log.txt");
      this.output = new DataOutputStream(f);
      this.fileName = dir.getAbsolutePath() + File.separatorChar + timeS + "_log.txt";
    } catch (IOException ex) {
      throw(new LogException("Warning : Couldn't to create file " + "log/"+timeS+"_log.txt", EnumCriticalityException.ERROR));
    }
  }
  //-------------------------------------------------------------------------
  public void printDebug(String message) {
    try {
      this.output.write(("[DEBUG] : " + message + "\n").getBytes());
      this.output.flush();
    } catch (IOException ex) {
      this.simpleLog.printDebug(message);
    }
  }
  public void printError(String message) {
    try {
      this.output.write(("[ERROR] : " + message + "\n").getBytes());
      this.output.flush();
    } catch (IOException ex) {
      this.simpleLog.printError(message);
    }
   
  }
  public void printInfo(String message) {
    try {
      this.output.write(("[INFO] : " + message + "\n").getBytes());
      this.output.flush();
    } catch (IOException ex) {
      this.simpleLog.printInfo(message);     
    }
  }
  public void printStackTrace(StackTraceElement[] trace) {
    if (trace == null)
      return;   
    try {
      this.output.write("[TRACE START]\n".getBytes());
      System.err.print("[TRACE START]\n");
      for (int i = 0; i < trace.length; i++) {
        this.output.write(("[TRACE] : " + trace[i].toString() + "\n").getBytes());
        System.err.print("[TRACE] : " + trace[i].toString() + "\n");
      }
      this.output.write("[TRACE END]\n".getBytes());
      System.err.print("[TRACE END]\n");
    } catch (IOException e) {
      this.simpleLog.printStackTrace(trace);
    }
  }
  public void printException(Throwable ex) {
    if (ex == null)
      return;   
    try {
      this.output.write(("[TRACE START] [" + ex.getMessage() + "]\n").getBytes());
      System.err.print(("[TRACE START] [" + ex.getMessage() + "]\n"));
      for (int i = 0; i < ex.getStackTrace().length; i++) {
        this.output.write(("[TRACE] : " + ex.getStackTrace()[i].toString() + "\n").getBytes());
        System.err.print("[TRACE] : " + ex.getStackTrace()[i].toString() + "\n");
      }
      this.output.write("[TRACE END]\n".getBytes());
      System.err.print("[TRACE END]\n");
    } catch (IOException e) {
      this.simpleLog.printException(ex);
    }   
  }
  public String getLoggerInfo() {
    String msg = "Log file : " + this.fileName;
    return(msg);
  }
}
TOP

Related Classes of vg.logging.FileLog

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.